﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AgileLogistics.View.Baoxiu
{
    /// <summary>
    /// ListPage.xaml 的交互逻辑
    /// </summary>
    public partial class ListPage : Page
    {
        Model.DBContext dbContext = new Model.DBContext();

        ViewModel.RepairViewModel vm = new ViewModel.RepairViewModel();


        private bool isAll = false;
        public ListPage(bool IsAll)
        {
            InitializeComponent();

            vm = DataContext as ViewModel.RepairViewModel;

            dp_Start.SelectedDate = DateTime.Now.Date;
            dp_End.SelectedDate = DateTime.Now.Date.AddDays(1).Date;
            isAll = IsAll; 
            //初始化数据
            if(IsAll)
            {
                vm.RepairList = dbContext.DB.Queryable<Model.Repairs>().Where(r=>r.State !="待处理").ToList();

               
            }
            else
            {
                txt_UserName.Visibility = Visibility.Collapsed;
                cmb_DepName.Visibility = Visibility.Collapsed;
                
                vm.RepairList = dbContext.DB.Queryable<Model.Repairs>().Where(r=>r.UserId== Comm.Temp.LoginUser.Id).ToList();
            }
            vm.DepList = dbContext.DB.Queryable<Model.Depment>().ToList();
        }

        private void Btn_select_Click(object sender, RoutedEventArgs e)
        {
            var query = dbContext.DB.Queryable<Model.Repairs>();

            if(!string.IsNullOrWhiteSpace( txt_UserName.Text))
            {
                query = query.Where(r => r.UserName.Contains( txt_UserName.Text));
            }

            if ( cmb_DepName.SelectedValue!=null)
            {
                query = query.Where(r => r.DepId == (int)cmb_DepName.SelectedValue);
            }
            if(dp_Start.SelectedDate!=null)
            {
                query = query.Where(r => r.CreateTime >=dp_Start.SelectedDate );
            }
            if (dp_End.SelectedDate != null)
            {
                query = query.Where(r => r.CreateTime < dp_End.SelectedDate);
            }

            if(Comm.Temp.LoginUser.RoleId!=1 && Comm.Temp.LoginUser.RoleId!=3)
            {
                query = query.Where(r=>r.UserId == Comm.Temp.LoginUser.Id);
            }


            vm.RepairList = query.ToList();
        }

       

        private void Btn_DaiChuli_Click(object sender, RoutedEventArgs e)
        {
            vm.RepairList =  dbContext.DB.Queryable<Model.Repairs>().Where(r=>r.State=="待处理").ToList();
        }

        private void Btn_ChuliZhong_Click(object sender, RoutedEventArgs e)
        {
            vm.RepairList = dbContext.DB.Queryable<Model.Repairs>().Where(r => r.State == "处理中").ToList();
        }

        private void Btn_Yichuli_Click(object sender, RoutedEventArgs e)
        {
            vm.RepairList = dbContext.DB.Queryable<Model.Repairs>().Where(r => r.State == "已完成").ToList();
        }

        private void Btn_ClearSelect_Click(object sender, RoutedEventArgs e)
        {
            txt_UserName.Text = "";
            cmb_DepName.SelectedItem = null;
            dp_Start.SelectedDate = DateTime.Now.Date;
            dp_End.SelectedDate = DateTime.Now.Date.AddDays(1).Date;


            if (isAll)
            {
                vm.RepairList = dbContext.DB.Queryable<Model.Repairs>().Where(r => r.State != "待处理").ToList();
            }else
            {
                vm.RepairList = dbContext.DB.Queryable<Model.Repairs>().Where(r => r.UserId == Comm.Temp.LoginUser.Id).ToList();
            }
               
        }
    }
}
